package edu.zjucst.spb.service.message.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.zjucst.spb.dao.message.UserMessageDao;
import edu.zjucst.spb.dao.message.UserMessageTextDao;
import edu.zjucst.spb.domain.dto.MessageQueryDTO;
import edu.zjucst.spb.domain.dto.MessageQueryMapper;
import edu.zjucst.spb.domain.entity.message.UserMessage;
import edu.zjucst.spb.domain.entity.message.UserMessageText;
import edu.zjucst.spb.domain.param.message.MessageQueryParam;
import edu.zjucst.spb.domain.vo.message.UserNotifyMessageVO;
import edu.zjucst.spb.service.message.UserMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * (UserMessage)表服务实现类
 *
 * @author Mrli
 * @since 2023-04-05 14:17:21
 */
@Service("userMessageService")
public class UserMessageServiceImpl extends ServiceImpl<UserMessageDao, UserMessage> implements UserMessageService {

    @Resource
    private UserMessageTextDao userMessageTextDao;
    @Resource
    private UserMessageDao userMessageDao;

    @Override
    public boolean sendPresetMessage(Long sendUserId, Long recvUserId, Long messageId, LocalDateTime dateTime) {
        return save(UserMessage.builder()
            .messageId(messageId)
            .sendUserId(sendUserId)
            .recvUserId(recvUserId)
            .needPush(true)
            .needPushTime(dateTime)
            .build()
        );
    }

    @Override
    public boolean sendPresetMessageRightNow(Long sendUserId, Long recvUserId, Long messageId) {
        return save(UserMessage.builder()
            .messageId(messageId)
            .sendUserId(sendUserId)
            .recvUserId(recvUserId)
            .needPush(false)
            .build()
        );
    }

    @Override
    @Transactional
    public boolean sendMessage(Long sendUserId, Long recvUserId, String content) {
        // 1. 创建一条 userMessageText
        UserMessageText userMessageText = new UserMessageText(content);
        // 插入成功后 主键id 会自动回显填入 userMessageText
        userMessageTextDao.insert(userMessageText);

        // 2. TODO: 这边得校验 sendUserId, recvUserId 是否合法(存在)
//        boolean existSend = userDao.exists(new LambdaQueryWrapper<SpbUser>().eq(SpbUser::getId, sendUserId));
//        boolean existRecv = userDao.exists(new LambdaQueryWrapper<SpbUser>().eq(SpbUser::getId, recvUserId));
//
//        if (!existSend || !existRecv) {
//            return false;
//        }

        // 3. 创建一条 userMessage, 其中 messageId 为刚创建 userMessageText 的 id
        return save(UserMessage.builder()
            .messageId(userMessageText.getId())
            .sendUserId(sendUserId)
            .recvUserId(recvUserId)
            .needPush(false)
            .build()
        );
    }


    @Override
    @Transactional
    public boolean sendMessageMessageToGroup(Long sendUserId, List<Long> userList, String content) {
        UserMessageText userMessageText = new UserMessageText(content);
        // 插入成功后 主键id 会自动回显填入 userMessageText
        userMessageTextDao.insert(userMessageText);

        // 2. TODO: 这边得校验 sendUserId, recvUserId 是否合法(存在)

        // 3. 给用户批量插入
        ArrayList<UserMessage> userMessages = new ArrayList<>();
        for (Long userId : userList) {
            userMessages.add(UserMessage.builder()
                .messageId(userMessageText.getId())
                .sendUserId(sendUserId)
                .recvUserId(userId)
                .needPush(false)
                .build());
        }
        return saveBatch(userMessages);
    }


    @Override
    public boolean updateBatchByIdList(Long sendUserId, List<Long> idList) {
        userMessageDao.updateIsReadByIdBatch(idList);
        return true;
    }


    public PageInfo<UserNotifyMessageVO> queryMessageByPage(Long recvUserId, MessageQueryParam param) {
        MessageQueryDTO dto = MessageQueryMapper.INSTANCE.param2dto(param);
        dto.setRecvUserId(recvUserId);
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<UserNotifyMessageVO> message = userMessageDao.queryMessage(dto);
        return new PageInfo<UserNotifyMessageVO>(message);
    }
}

